package com.py.credit.controller;

import java.io.IOException;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.py.credit.ai.client.IAipFace;
import com.py.credit.ai.constant.AipFaceConstant;
import com.py.credit.entity.Firm;
import com.py.credit.entity.Report;
import com.py.credit.service.UserManagerService;
import com.py.framework.core.common.Page;
import com.py.framework.core.rest.support.RestServiceResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api
@RestController
@RequestMapping("/endpoint")
public class EndPointController {

	@Value("${ai.face.group}")
	private String groupId;

	@Autowired
	IAipFace aipFaceManager;

	@Autowired
	UserManagerService userManagerService;

	@ApiOperation(value = "人脸认证")
	@RequestMapping(value = "/verifyFace.json", method = RequestMethod.POST)
	public RestServiceResult<Boolean> verfyUser(
			@ApiParam(required = true, name = "image", value = "人脸图片") @RequestParam(required = true, value = "image") String image,
			@ApiParam(required = true, name = "idNumber", value = "身份证号") @RequestParam(required = true, value = "idNumber") String idNumber,
			@ApiParam(required = true, name = "accessToken", value = "访问凭据") @RequestParam(required = true, value = "accessToken") String accessToken) {

		return new RestServiceResult<Boolean>(
				aipFaceManager.verifyUser(image, AipFaceConstant.IMAGE_TYPE_BASE64, groupId, idNumber));

	}

	@ApiOperation(value = "获取授权企业列表信息")
	@RequestMapping(value = "/getAuthorizedFirms.json", method = RequestMethod.POST)
	public RestServiceResult<List<Firm>> getAuthorizedFirms(
			@ApiParam(required = true, name = "idNumber", value = "身份证号") @RequestParam(required = true, value = "idNumber") String idNumber,
			@ApiParam(required = true, name = "accessToken", value = "访问凭据") @RequestParam(required = true, value = "accessToken") String accessToken,
			Page page)

	{

		List<Firm> authorizations = userManagerService.getAuthorizedFirms(idNumber, page);

		return new RestServiceResult<List<Firm>>(page.getTotalCount(), authorizations);

	}

//	@ApiOperation(value = "获取企业报告列表")
//	@RequestMapping(value = "/getFirmReports.json", method = RequestMethod.POST)
//	public RestServiceResult<List<Report>> getFirmReports(
//			@ApiParam(required = true, name = "tybm", value = "统一编码") @RequestParam(required = true, value = "tybm") String tybm,
//			@ApiParam(required = true, name = "accessToken", value = "访问凭据") @RequestParam(required = true, value = "accessToken") String accessToken,
//			Page page)
//
//	{
//
//		Report credit = Report.builder().id(1).reportName("金电联行（北京）信息技术有限公司信用报告").build();
//
//		Report check = Report.builder().id(2).reportName("金电联行（北京）信息技术有限公司核查报告").build();
//
//		return new RestServiceResult<List<Report>>(2, Lists.newArrayList(credit, check));
//
//	}

	@ApiOperation(value = "获取企业报告文件")
	@RequestMapping(value = "/getFirmReportFile.json", method = RequestMethod.POST)
	public RestServiceResult<String> getFirmReportFile(
			@ApiParam(required = true, name = "id", value = "报告编号") @RequestParam(required = true, value = "id") String id,
			@ApiParam(required = true, name = "accessToken", value = "访问凭据") @RequestParam(required = true, value = "accessToken") String accessToken)
			throws IOException

	{

		return new RestServiceResult<String>(
				Base64Utils.encodeToString(IOUtils.toByteArray(this.getClass().getResource("/Credit_Report.pdf"))));

	}

}
